﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Rose.Helper;
using Rose.Models;

namespace Rose.Controllers
{
    
    [Authorize]
    public class FriendTrendController : Controller
    {
        private Database_RoseEntities db = new Database_RoseEntities();

        // 主页
        public ActionResult Friend(string friendId)
        {
            return View();
        }

        #region  各种评论功能

             //case "article": return 0;
             //   case "share": return 1;
             //   case "album" :return 2;
             //   case "feeling": return 3;

        #region Add

        [Authorize]
        [HttpPost]
        public ActionResult AddComment(string Id,int type, string comment)
        {
            try
            {
                if (type == 3)
                {
                    string selfId = new CheckHelpModel().CheckCurrentUserId();
                    CommentModel returnModel= new CommentHelpModel().AddFeelingCommentToDatabase(selfId, Id, comment);
                    return Json(returnModel);
                }

                //type=1 //article
                {
                    string selfId = new CheckHelpModel().CheckCurrentUserId();
                    CommentModel returnModel = new CommentHelpModel().AddArticleCommentToDatabase(selfId, Id, comment);
                    return Json(returnModel);
                }



            }
            catch (Exception ex)
            {
                return Content("错误-" + ex.Message);

            }
        }
     

        #endregion


        #region Delete
        [Authorize]
        [HttpPost]
        public ActionResult DeleteComment(string Id, int type)
        {
            try
            {
                if (type == 3)
                {
                    string selfId = new CheckHelpModel().CheckCurrentUserId();
                    new CommentHelpModel().RemoveFeelingCommentFromDatabase(Id, selfId);
                    return Content("删除成功");
                }
                else
                //文章的删除
                {
                    string selfId = new CheckHelpModel().CheckCurrentUserId();
                    new CommentHelpModel().RemoveArticleCommentFromDatabase(Id, selfId);
                    return Content("删除成功");
                }
            }
            catch (Exception ex)
            {
                return Content("错误-" + ex.Message);
            }
        }
      

        #endregion


        #region  GetList

     


        [Authorize]
        public ActionResult GetCommentListJson(string Id, int type)
        {
            if (type == 0)
            {
                List<CommentModel> returnModel = new ArticleCommentHelpModel().GetCommentModelList(Id,false);
                return Json(returnModel, JsonRequestBehavior.AllowGet);
            }
            if (type == 3)
            {
                List<CommentModel> returnModel = new FeelingCommentHelpModel().GetCommentModelList(Id,false);
                return Json(returnModel, JsonRequestBehavior.AllowGet);
            }
            return Json(null, JsonRequestBehavior.AllowGet);
        }
     
 

        #endregion


        #endregion


        #region 好友心情动态

        private FeelingHelpModel feelinghelpmodel = new FeelingHelpModel();

        [Authorize]
        public ActionResult FriendFeelingList(string friendId, int currentpage = 1)
        {
            ViewData["PageTitle"] = "Show All Feelings";
            const int PAGESIZE = 30;
            ViewData["FriendId"] = friendId;
            ViewData["FriendName"] = new CheckHelpModel().CheckUserInfo(friendId).Name;
            ViewData["Type"] = ReturnImageHelpModel.GetFeelImage("all");
            int allcount = feelinghelpmodel.GetFeelingCount(friendId);
            ViewData["AllCount"] = allcount;
            ViewData["MaxPage"] = allcount % PAGESIZE == 0 ? allcount / PAGESIZE : (allcount / PAGESIZE) + 1;

            List<FeelingModel> model = feelinghelpmodel.GetFeelingModelList(friendId, currentpage, PAGESIZE);
            return View(model);
        }

        [Authorize]
        [HttpPost]
        public ActionResult FriendFeelingList(string friendId,string type, DateTime? minTime, DateTime? maxTime)
        {
            //在使用post方法的时候，限定了时间范围，所以没有实现分页功能;
            ViewData["PageTitle"] = "Show All Feelings";
            ViewData["FriendId"] = friendId;
            const int PAGESIZE = int.MaxValue;
            int currentpage = 1;


            int allcount = 0;


            List<FeelingModel> model = new List<FeelingModel>();
            if (string.IsNullOrEmpty(type) || type == "all")
            {
                ViewData["Type"] = ReturnImageHelpModel.GetFeelImage("all");
                if (minTime == null && maxTime == null)
                {
                    model = feelinghelpmodel.GetFeelingModelList(friendId, currentpage, PAGESIZE);
                    allcount = feelinghelpmodel.GetFeelingCount(friendId);
                }
                else if (maxTime == null)
                {
                    model = feelinghelpmodel.GetFeelingModelList(friendId, currentpage, PAGESIZE, minTime ?? DateTime.MinValue);
                    allcount = feelinghelpmodel.GetFeelingCount(friendId, minTime ?? DateTime.MinValue);
                }
                else if (minTime == null)
                {
                    model = feelinghelpmodel.GetFeelingModelList(friendId, currentpage, PAGESIZE, DateTime.MinValue, maxTime ?? DateTime.MaxValue);
                    allcount = feelinghelpmodel.GetFeelingCount(friendId, DateTime.MinValue, maxTime ?? DateTime.MaxValue);
                }
                else
                {
                    model = feelinghelpmodel.GetFeelingModelList(friendId, currentpage, PAGESIZE, minTime ?? DateTime.MinValue, maxTime ?? DateTime.MaxValue);
                    allcount = feelinghelpmodel.GetFeelingCount(friendId, minTime ?? DateTime.MinValue, maxTime ?? DateTime.MaxValue);
                }
            }

            else
            {
                ViewData["Type"] = ReturnImageHelpModel.GetFeelImage(type);
                if (minTime == null && maxTime == null)
                {
                    model = feelinghelpmodel.GetTypeFeelingModelList(friendId, type, currentpage, PAGESIZE);
                    allcount = feelinghelpmodel.GetTypeFeelingSomeCount(friendId, type);
                }
                else if (maxTime == null)
                {
                    model = feelinghelpmodel.GetTypeFeelingModelList(friendId, type, currentpage, PAGESIZE, minTime ?? DateTime.MinValue);
                    allcount = feelinghelpmodel.GetTypeFeelingSomeCount(friendId, type, minTime ?? DateTime.MinValue);
                }
                else if (minTime == null)
                {
                    model = feelinghelpmodel.GetTypeFeelingModelList(friendId, type, currentpage, PAGESIZE, DateTime.MinValue, maxTime ?? DateTime.MaxValue);
                    allcount = feelinghelpmodel.GetTypeFeelingSomeCount(friendId, type, DateTime.MinValue, maxTime ?? DateTime.MaxValue);
                }
                else
                {
                    model = feelinghelpmodel.GetTypeFeelingModelList(friendId, type, currentpage, PAGESIZE, minTime ?? DateTime.MinValue, maxTime ?? DateTime.MaxValue);
                    allcount = feelinghelpmodel.GetTypeFeelingSomeCount(friendId, type, minTime ?? DateTime.MinValue, maxTime ?? DateTime.MaxValue);
                }
            }

            ViewData["AllCount"] = allcount;
            ViewData["MaxPage"] = allcount % PAGESIZE == 0 ? allcount / PAGESIZE : allcount / PAGESIZE + 1;
            return View("FriendFeelingList",model);


        }
        #endregion


        #region friend 文章

        [Authorize]
        public ActionResult FriendArticleList(string friendId,int currentpage=1)
        {
            
            const int PAGESIZE = 20;
            int allcount = new ArticleHelpModel().GetArticleCount(friendId, true);
            ViewData["AllCount"] = allcount;
            ViewData["MaxPage"] = allcount % PAGESIZE == 0 ? allcount / PAGESIZE : allcount / PAGESIZE + 1;
            List<ArticleAbstractModel> model = new ArticleHelpModel().GetArticleAbstractModelList(friendId,true,currentpage,PAGESIZE);
            return View(model);
        }

        [Authorize]
        [HttpPost]
        public ActionResult FriendArticle(string articleId)
        {
            //null 表示自己
            //true 表示朋友
            //false 表示任何人
            return Json(new ArticleHelpModel().GetArticleModel(articleId,true));
        }

        #endregion



        #region Friend //这个是首页
        [Authorize]
        public ActionResult FriendTrend(string friendId,int currentpage=1)
        {
            const int PAGESIZE = int.MaxValue;
            List<NewTrendModel> returnModel =new NewTrendHelpModel().GetAFriendTrendModelList(friendId,currentpage, PAGESIZE);
            return View(returnModel);
        }


        #endregion

        #region Friend Info
 
        [Authorize]
        public ActionResult FriendInfo(string friendId)
        {

            UserInfo userInfo = new CheckHelpModel().CheckUserInfo(friendId);
            //UserInfo userInfo = db.UserInfos.Where(c => c.Id == friendId).FirstOrDefault();
            //if (userInfo == null)
            //    throw new ArgumentNullException();
            UserInfoModel returnModel = new UserInfoModel()
            {
                Gender = userInfo.Gender,
                

                University = userInfo.University,
                College = userInfo.College ?? string.Empty,
                Major = userInfo.Major,
                ClassRoom = userInfo.ClassRoom ?? string.Empty,
                SchoolYear = userInfo.SchoolYear,

                UserName = HttpContext.User.Identity.Name,

                ////分割
                Email = userInfo.Email ?? string.Empty,
               
                AddressProvince = userInfo.AddressProvince ?? string.Empty,
                AddressCity = userInfo.AddressCity ?? string.Empty,
                AddressTown = userInfo.AddressTown ?? string.Empty,
                AddressStreet = userInfo.AddressStreet ?? string.Empty,

                BirthYear = userInfo.BirthDate == null ? 0 : userInfo.BirthDate.Value.Year,
                BirthMonth = userInfo.BirthDate == null ? 0 : userInfo.BirthDate.Value.Month,
                BirthDate = userInfo.BirthDate == null ? 0: userInfo.BirthDate.Value.Day,

                ImagePath = userInfo.ImagePath ?? string.Empty,
                QQ = userInfo.QQ ?? string.Empty,
                WebSite = userInfo.WebSite ?? string.Empty,
                
               
                TrueName = userInfo.Name ?? string.Empty,
                

            };

                       
            return View(returnModel);
        }
        public ActionResult FriendInfoPart(string friendId)
        {
            UserInfo userInfo =new  CheckHelpModel().CheckUserInfo(friendId);
            UserInfoModel returnModel = new UserInfoModel()
            {
                Gender = userInfo.Gender,

                University = userInfo.University,
                College = userInfo.College ?? string.Empty,
                Major = userInfo.Major,
                ClassRoom = userInfo.ClassRoom ?? string.Empty,
                SchoolYear = userInfo.SchoolYear,

                UserName = HttpContext.User.Identity.Name,

                ////分割
                Email = userInfo.Email ?? string.Empty,

                AddressProvince = userInfo.AddressProvince ?? string.Empty,
                AddressCity = userInfo.AddressCity ?? string.Empty,
                AddressTown = userInfo.AddressTown ?? string.Empty,
                AddressStreet = userInfo.AddressStreet ?? string.Empty,

                BirthYear = userInfo.BirthDate == null ? 0 : userInfo.BirthDate.Value.Year,
                BirthMonth = userInfo.BirthDate == null ? 0 : userInfo.BirthDate.Value.Month,
                BirthDate = userInfo.BirthDate == null ? 0 : userInfo.BirthDate.Value.Day,

                ImagePath = userInfo.ImagePath ?? string.Empty,
                QQ = userInfo.QQ ?? string.Empty,
                WebSite = userInfo.WebSite ?? string.Empty,

                TrueName = userInfo.Name ?? string.Empty,


            };


            return View(returnModel);
        }

        #endregion

        #region  Friend ALbum

        //查看所有相册

        [Authorize]
        public ActionResult FriendAlbumList(string friendId)
        {
           
            var returnModel = new AlbumHelpModel().GetAlbumModels(friendId,true);
            return View(returnModel);
        }

        public ActionResult FriendAlbum(string friendId, string albumId)
        {
            List<PhotoModel> returnModel=new List<PhotoModel>();
            Album album = db.Albums.Where(c => c.UserId == friendId && c.Id == new Guid(albumId) && c.IsOnlyForFriend != null).FirstOrDefault();
            if (album == null)
                return Json(returnModel, JsonRequestBehavior.AllowGet);
            returnModel =new  AlbumHelpModel().GetAlbumPhotos(album.Id.ToString(),friendId);
            return Json(returnModel, JsonRequestBehavior.AllowGet);
                
        }

       

        public ActionResult PublicAlbum(string friendId, string albumId)
        {
            List<PhotoModel> returnModel = new List<PhotoModel>();
            Album album = db.Albums.Where(c => c.UserId == friendId && c.Id == new Guid(albumId) && c.IsOnlyForFriend == false).FirstOrDefault();
            if (album == null)
                return Json(returnModel, JsonRequestBehavior.AllowGet);
            returnModel = new AlbumHelpModel().GetAlbumPhotos(album.Id.ToString(),friendId);
            return Json(returnModel, JsonRequestBehavior.AllowGet);
        }
       

        #endregion

        #region FriendShare
        [Authorize]
        public ActionResult FriendShareList(string friendId, int currentpage)
        {
            const int PAGESIZE = 20;
            int allcount = new ShareHelpModel().GetShareCount(friendId);
            ViewData["AllCount"] = allcount;
            ViewData["MaxPage"] = allcount % PAGESIZE == 0 ? allcount / PAGESIZE : allcount / PAGESIZE + 1;
            List<Share> model = new ShareHelpModel().GetShareList(friendId, currentpage, PAGESIZE);

            return View(model);
        
        }

        #endregion


      

    }
}
